package com.emm.yixun.jobs;

import java.util.Date;

import com.emm.yixun.system.service.pmo.PmoDynamicDataServiceFacade;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.emm.yixun.common.enums.ResponseEnums;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.ProjectPmo;
import com.emm.yixun.common.model.dto.PmoLogDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.dto.ProjectPmoDto;
import com.emm.yixun.common.model.pmo.LevelDynamiSalesData;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.common.utils.MD5;
import com.emm.yixun.common.utils.RequestUtil;
import com.emm.yixun.common.utils.ResponseBody;
import com.emm.yixun.estate.service.ProjectPmoServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.jobs.config.PmoConfig;
import com.emm.yixun.system.service.PmoLogServiceFacade;

@Component
public class PmoDataSendJob {
	private static Logger logger = LoggerFactory.getLogger(PmoDataSendJob.class);
	@Autowired
	private ProjectServiceFacade projectServiceFacade;

	@Autowired
	private ProjectPmoServiceFacade projectPmoServiceFacade;

	@Autowired
	private PmoConfig pmoConfig;

	@Autowired
	private PmoLogServiceFacade pmoLogServiceFacade;
	@Autowired
	private PmoDynamicDataServiceFacade pmoDynamicDataServiceFacade;

	@Scheduled(cron = "${quartz.cron.pmoDataSendJob}")
	public void pmoDataSend() {
		Date now = new Date();
		String month = DateUtils.DateToString(now, DateStyle.YYYYMM);
		ProjectDto dto = new ProjectDto();
		ResponseList<Project> list = projectServiceFacade.findByDto(dto);
		if (list.isSuccess() && list.getEntity().size() > 0) {
			try{
				for (Project project : list.getEntity()) {
					ProjectPmoDto projectPmoDto = new ProjectPmoDto();
					projectPmoDto.setProjectId(project.getId());
					ResponseList<ProjectPmo> projectpmoList = projectPmoServiceFacade.findByDto(projectPmoDto);
					if(projectpmoList==null || projectpmoList.getEntity()==null || projectpmoList.getEntity().size()<= 0) {
						continue;
					}
					String pmoProjectCode = projectpmoList.getEntity().get(0).getPmoProjectCode();
					if (StringUtils.isBlank(pmoProjectCode))
						continue;

					Response<LevelDynamiSalesData> response = pmoDynamicDataServiceFacade.quereyLevelDynamiSalesData(pmoProjectCode, month);
					//Response<LevelDynamiSalesData> response = projectServiceFacade.quereyLevelDynamiSalesData(pmoProjectCode, month);
					if (!response.isSuccess()) {
						continue;
					}
					boolean pmoPushFlag = projectServiceFacade.checkPmoPush(pmoProjectCode);
					if(!pmoPushFlag){
						continue;
					}

					LevelDynamiSalesData levelDynamiSalesData = response.getEntity();
					String json = JSON.toJSONString(levelDynamiSalesData);
					String clientKey = pmoConfig.getClientKey();
					String seq = pmoConfig.getSeq();
					String digest = MD5.md5(clientKey + seq + pmoConfig.getClientPwd()).toUpperCase();
					String url = pmoConfig.getRequest_url() + "/api/v1/pmo/project-dynamic-sale/" + pmoProjectCode;
					ResponseBody responseBody = RequestUtil.buildRequest(json, url, clientKey, seq, digest);
					if (!responseBody.getCode().equals("000000")) {
						logger.error("PMO动态销售数据推送错误：" + responseBody.getDesc());
					}
					PmoLogDto pmoLogDto = new PmoLogDto();
					pmoLogDto.setMerchantId(Long.valueOf(1));
					pmoLogDto.setDirection("1");
					if (responseBody.getCode().equals(ResponseEnums.RESPONSEENUMS_000000.getCode())) {// 成功
						pmoLogDto.setStatus("1");
					} else {
						pmoLogDto.setStatus("2");
					}
					pmoLogDto.setRequestParams(json);
					pmoLogDto.setDelFlag("0");
					pmoLogDto.setInterfaceName("销售动态信息");
					pmoLogDto.setReturnResult(JSON.toJSONString(responseBody));
					pmoLogServiceFacade.save(pmoLogDto);
				}
			}catch(Exception ex){
				logger.error("PMO动态销售数据推送异常：", ex);
			}
		}

	}
}
